<?php
namespace app\admin\controller;

use app\admin\code\AdminCode;
use app\admin\code\BaseCode;
use app\admin\logic\AdminLogic;
use app\admin\logic\LoginLogic;
use app\admin\model\AdminModel;
use app\admin\util\Response;
use app\admin\validate\AdminValidate;
use think\Lang;
use think\Request;
use think\Session;

class Admin extends Base
{
    public function edit_password(){
        if($this->request->isAjax()){
            $param = [];
            $param['id'] = Session::get('admin.id');
            $param['pass_word'] = Request::instance()->request('old_password','','htmlspecialchars');
            $param['new_password'] = Request::instance()->request('new_password','','htmlspecialchars');
            $param['confirm_password'] = Request::instance()->request('confirm_password','','htmlspecialchars');
            if($param['confirm_password'] != $param['new_password']){
                Response::json(AdminCode::TWO_PASSWORD_IS_WRONG,Lang::get(AdminCode::TWO_PASSWORD_IS_WRONG));
            }
            $logic = new AdminLogic($param);
            $logic->edit_password(new AdminValidate(new AdminModel()));
            if($logic->result === false){
                Response::json($logic->err['code'],$logic->err['msg']);
            }else{
                Response::json();
            }
        }else{
            $admin_info = Session::get('admin.id');
            if(!$admin_info){
                $this->error('请先登录',url('admin/loing/index'));
            }
            $this->assign('id',Session::get('admin.id'));
            return view();
        }
    }
    public function logout(){
        Session::delete('admin');
        $this->redirect(url('login/index'));
    }
}
